#!/usr/bin/env python

import os
import sys
import subprocess
import random

line = "--------------------"

print line
print "MAC ADDRESS RANDOMIZER"

#GET INTERFACE LIST
print line
print "Select interface device"
print line
file_num = 0
#get set iface as list
iface = []
#for each file in this dir
for file in os.listdir("/sys/class/net/"):
	#add the file to the list
	iface.append(file)
	#print the file_num, and the interface with the selected file_num
	print file_num, iface[file_num]
	#add one to the file_num count for the next loop
	file_num = file_num + 1

#SELECT IFACE
print line
iface_num = input("Please choose an interface number:\n")
if iface_num > file_num - 1:
	print line
	sys.exit("No interface selected!")
else:
	iface = iface[iface_num]

#DISABLE IFACE
print line
print "Disabling " + iface
#os.system("ifconfig " + iface +" down")
print("ifconfig " + iface +" down")

#GENERATE RANDOM MAC
rnd_mac = ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F")
rnd_mac = ''.join([random.choice(rnd_mac) for _ in range(10)])
n = 2
rnd_mac = [rnd_mac[i:i+n] for i in range(0, len(rnd_mac), n)]
mac_value = 0
final_mac = ""
#for each item in the rnd_mac list
for item in rnd_mac:
	#set the final_mac to the rnd_mac plus colon
	final_mac = rnd_mac[mac_value] + ":" + final_mac
	#add one to mac_value
	mac_value = mac_value + 1
final_mac = "00:" + final_mac[:-1]


#SET RANDOM MAC
print line
print("Setting mac address to " + final_mac)
#os.system("ifconfig " + iface + " hw ether " + rnd_mac)
print("ifconfig " + iface + " hw ether " + final_mac)


#ENABLE IFACE
print line
print("Enabling " + iface)
#os.system("ifconfig " + iface + " up")
print("ifconfig " + iface +" up")

print line
print("All done!")
print line
